package io.github.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.github.dao.SysCallControlDao;
import io.github.entity.SysCallControlEntity;
import io.github.service.SysCallControlService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: weil
 * @date: 2019/9/19 13:49
 * @version: 1.0
 * @description:
 */
@Service
public class SysCallControlServiceImpl extends ServiceImpl<SysCallControlDao, SysCallControlEntity> implements SysCallControlService {

    @Resource
    private SysCallControlDao dao;

    @Override
    public Page<SysCallControlEntity> queryListByPage(Integer offset, Integer limit, String sort,
                                                      Boolean order) {
        Wrapper<SysCallControlEntity> wrapper = new EntityWrapper<SysCallControlEntity>();

        wrapper.orderBy("id",false);

        Page<SysCallControlEntity> page = new Page<>(offset, limit);
        return this.selectPage(page, wrapper);
    }

    @Override
    public Page<SysCallControlEntity> queryListByPage(Integer offset, Integer limit,String gatewayFrom,String gateway) {
        Wrapper<SysCallControlEntity> wrapper = new EntityWrapper<SysCallControlEntity>();

        if(StringUtils.isNotBlank(gatewayFrom)){
            wrapper.like("from_name",gatewayFrom);
        }
        if(StringUtils.isNotBlank(gateway)){
            wrapper.like("gateway",gateway);
        }

        wrapper.orderBy("id",false);
        Page<SysCallControlEntity> page = new Page<>(offset, limit);
        return this.selectPage(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysCallControlEntity router) {
        baseMapper.insert(router);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] conIds) {
        //baseMapper.deleteBatch(conIds);

        baseMapper.deleteBatchIds(Arrays.asList(conIds));
    }

    @Override
    public void updateRouter(SysCallControlEntity entity, Long conId) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id",conId);
        baseMapper.update(entity,wrapper);
    }

    @Override
    public List<SysCallControlEntity> queryAllRouter() {
        return baseMapper.queryAllRouter();
    }

    @Override
    public List<SysCallControlEntity> queryByAreaid(String areaid) {
        return baseMapper.queryByAreaid(areaid);
    }

    @Override
    public void deleteByTel(String tel) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("tel_to",tel);
        baseMapper.delete(wrapper);
    }

    @Override
    public void deleteByReqid(String reqid) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("reqid",reqid);
        baseMapper.delete(wrapper);
    }

}
